package com.xmb.common.service.impl;

import com.xmb.common.dao.XmbUserBalanceBlotterMapper;
import com.xmb.common.entity.XmbUserBalanceBlotter;
import com.xmb.common.entity.reward.ActivityRanking;
import com.xmb.common.service.UserBalanceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * Created by lyndon on 2019/4/10.
 */
@Service
public class UserBalanceServiceImpl implements UserBalanceService{

    @Resource
    private XmbUserBalanceBlotterMapper balanceBlotterMapper;

    /**
     * 获取用户的余额总金额
     * @param uid
     * @return
     */
    public BigDecimal getTotalBalance(Long uid) {
        return Optional.ofNullable(balanceBlotterMapper.getTotalBalance(uid)).orElse(BigDecimal.ZERO);
    }

    /**
     * 保存余额流水
     * @param record
     * @return
     */
    public int saveBalanceBlotter(XmbUserBalanceBlotter record) {
        return balanceBlotterMapper.insertSelective(record);
    }

    @Override
    public List<XmbUserBalanceBlotter> getBalanceList(Long userId, Long typeId, String exchangeType) {
        XmbUserBalanceBlotter balanceBlotter = new XmbUserBalanceBlotter();
        balanceBlotter.setExchangeType(exchangeType);
        balanceBlotter.setUserId(userId);
        balanceBlotter.setTypeId(typeId);
        return balanceBlotterMapper.getBalanceList(balanceBlotter);
    }

    @Override
    public List<ActivityRanking> getBalanceRanking(Long typeId) {
        return balanceBlotterMapper.getBalanceRanking(typeId);
    }

    @Override
    public ActivityRanking getCurrentUserActivityRewardRanking(Long activityId, Long userId) {
        XmbUserBalanceBlotter balanceBlotter = new XmbUserBalanceBlotter();
        balanceBlotter.setUserId(userId);
        balanceBlotter.setTypeId(activityId);
        return balanceBlotterMapper.getCurrentUserActivityRewardRanking(balanceBlotter);
    }
}
